import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import typeinfo.pets.Cymric;
import typeinfo.pets.Hamster;
import typeinfo.pets.Mouse;
import typeinfo.pets.Pet;
import typeinfo.pets.Pets;

public class E05_IntegerListFeatures {
	public static void main(String[] args) {
		Random pRand = new Random(47);
		Random iRand = new Random(47);
		List<Pet> pets = Pets.arrayList(7);
		List<Integer> ints = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4,
				5, 6, 7));
		System.out.println("p1: " + pets);
		System.out.println("i1: " + ints);
		Hamster h = new Hamster();
		Integer i = 8;
		pets.add(h);
		ints.add(i);
		System.out.println("2: " + pets);
		System.out.println("2: " + ints);
		System.out.println("3: " + pets.contains(h));
		System.out.println("3: " + ints.contains(i));
		pets.remove(h);
		ints.remove(i);
		Pet p = pets.get(2);
		Integer i2 = ints.get(2);
		System.out.println("4: " + p + " " + pets.indexOf(p));
		System.out.println("4: " + i2 + " " + ints.indexOf(i2));
		Cymric c = new Cymric();
		Integer i3 = Integer.valueOf(3);
		System.out.println("5: " + pets.indexOf(c));
		System.out.println("5: " + ints.indexOf(i3));
		System.out.println("6: " + pets.remove(c));
		System.out.println("6: " + ints.remove(i3));
		System.out.println("7: " + pets.remove(p));
		System.out.println("7: " + ints.remove(i2));
		System.out.println("8: " + pets);
		System.out.println("8: " + ints);
		pets.add(3, new Mouse());
		ints.add(3, Integer.valueOf(8));
		System.out.println("9: " + pets);
		System.out.println("9: " + ints);
		List<Pet> pSub = pets.subList(1, 4);
		List<Integer> iSub = ints.subList(1, 4);
		System.out.println("subList" + pSub);
		System.out.println("subList" + iSub);
		System.out.println("10: " + pets.contains(pSub));
		System.out.println("10: " + ints.contains(iSub));
		Collections.sort(pSub);
		Collections.sort(iSub);
		System.out.println("sorted subList: " + pSub);
		System.out.println("sorted subList: " + iSub);
		System.out.println("11: " + pets.containsAll(pSub));
		System.out.println("11: " + ints.containsAll(iSub));
		Collections.shuffle(pSub, pRand);
		Collections.shuffle(iSub, iRand);
		System.out.println("shuffled subList: " + pSub);
		System.out.println("shuffled subList: " + iSub);
		System.out.println("12:　" + pets.containsAll(pSub));
		System.out.println("12:　" + ints.containsAll(iSub));
		List<Pet> pCopy = new ArrayList<Pet>(pets);
		List<Integer> iCopy = new ArrayList<Integer>(ints);
		pSub = Arrays.asList(pets.get(1), pets.get(4));
		iSub = Arrays.asList(ints.get(1), ints.get(4));
		System.out.println("sub: " + pSub);
		System.out.println("sub: " + iSub);
		pCopy.retainAll(pSub);
		iCopy.retainAll(iSub);
		System.out.println("13: " + pCopy);
		System.out.println("13: " + iCopy);
		pCopy = new ArrayList<Pet>(pets);
		iCopy = new ArrayList<Integer>(ints);
		pCopy.remove(2);
		iCopy.remove(2);
		System.out.println("14: " + pets + pCopy);
		System.out.println("14: " + ints + iCopy);
		pCopy.removeAll(pSub);
		iCopy.removeAll(iSub);
		System.out.println("15: " + pCopy);
		System.out.println("15: " + iCopy);
		pCopy.set(1, new Mouse());// 最好使用replace
		iCopy.set(1, Integer.valueOf(2));// 最好使用replace
		System.out.println("16: " + pCopy);
		System.out.println("16: " + iCopy);
		pCopy.addAll(2, pSub);
		iCopy.addAll(2, iSub);
		System.out.println("17: " + pCopy);
		System.out.println("17: " + iCopy);
		System.out.println("18: " + pets.isEmpty());
		System.out.println("18: " + ints.isEmpty());
		pets.clear();
		ints.clear();
		System.out.println("19: " + pets);
		System.out.println("19: " + ints);
		System.out.println("20: " + pets.isEmpty());
		System.out.println("20: " + ints.isEmpty());
		pets.addAll(Pets.arrayList(4));
		ints.addAll(new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4)));
		System.out.println("21: " + pets);
		System.out.println("21: " + ints);
		Object[] po = pets.toArray();
		Object[] io = ints.toArray();
		System.out.println("22: " + po[3]);
		System.out.println("22: " + io[3]);
		// toArray(new Object[0]) 和 toArray()在功能上是相同的。
		Pet[] pa = pets.toArray(new Pet[0]);
		// toArray(new Object[0]) 和toArray()在功能上是相同的。
		Integer[] ia = ints.toArray(new Integer[0]);
		System.out.println("23: " + pa[3].id());
		System.out.println("23: " + ia[3]);
	}
}
